package com.unittec.drainage.provider.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unittec.drainage.provider.auth.entity.SysRole;
import com.unittec.drainage.provider.auth.entity.SysRolePermission;
import com.unittec.drainage.provider.auth.entity.SysUserRole;
import com.unittec.drainage.provider.auth.mapper.SysRoleMapper;
import com.unittec.drainage.provider.root.ServiceRoot;
import com.unittec.drainage.provider.utils.CollectionUtil;
import com.unittec.drainage.sdk.auth.request.Req101201;
import com.unittec.drainage.sdk.auth.request.Req101208;
import com.unittec.drainage.sdk.auth.request.Req101209;
import com.unittec.drainage.sdk.root.common.CommonConstant;
import com.unittec.drainage.sdk.root.internal.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @description 角色Service
 * @author Jason
 * @date 2019/11/07
 */
@Service
public class SysRoleService extends ServiceRoot<SysRoleMapper, SysRole> {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    /**
     * 角色分页列表
     *
     * @param req101201
     * @return
     */
    public IPage<Map<String, Object>> selectSysRolePage(Req101201 req101201) {
        QueryWrapper<SysRole> qw = new QueryWrapper<SysRole>();
        qw.eq("1", "1");
        if (StringUtils.isNotEmpty(req101201.getRoleName())) {
            qw.like("role_name", req101201.getRoleName());
        }
        if (StringUtils.isNotEmpty(req101201.getRoleCode())) {
            qw.like("role_code", req101201.getRoleCode());
        }
        if (req101201.getStartTime() != null && req101201.getEndTime() != null) {
            qw.between("create_time", req101201.getStartTime(), req101201.getEndTime());
        }
        qw.orderByDesc("create_time");
        return getMapper().selectSysRoleList(new Page<SysRole>(req101201.getCurrent(), req101201.getSize()), qw);
    }

    /**
     * 批量删除角色
     *
     * @param idList
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteSysRole(List<String> idList) {
        for (String id : idList) {
            if (CommonConstant.DEFAULT_ROLE.contains(id)) {
                throw new BusinessException("系统默认角色,无法删除");
            }
            QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
            queryWrapper.eq("role_id", id);
            List<SysUserRole> list = sysUserRoleService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                throw new BusinessException("角色已关联用户,无法删除");
            }
        }
        try {
            getMapper().deleteBatchIds(idList);
            LambdaQueryWrapper<SysUserRole> query1 = new LambdaQueryWrapper<SysUserRole>();
            query1.in(SysUserRole::getRoleId, idList);
            sysUserRoleService.remove(query1);
            LambdaQueryWrapper<SysRolePermission> query2 = new LambdaQueryWrapper<SysRolePermission>();
            query2.in(SysRolePermission::getRoleId, idList);
            sysRolePermissionService.remove(query2);
        } catch (Exception e) {
            throw new BusinessException("批量删除角色失败");
        }
    }

    public List<Map<String, Object>> selectSysRoleList(Req101208 req101208) {
        QueryWrapper<SysRole> qw = new QueryWrapper<SysRole>();
        qw.eq("1", "1");
        if (StringUtils.isNotEmpty(req101208.getRoleName())) {
            qw.like("role_name", req101208.getRoleName());
        }
        if (StringUtils.isNotEmpty(req101208.getRoleCode())) {
            qw.like("role_code", req101208.getRoleCode());
        }
        qw.orderByDesc("create_time");
        return getMapper().selectSysRoleList(qw);
    }

    public List<Map<String, Object>> getRoleListByIds(Req101209 content) {
        QueryWrapper<SysRole> qw = new QueryWrapper<SysRole>();
        qw.eq("1", "1");
        if (CollectionUtil.isNotEmpty(content.getRoleids())) {
            qw.in("id", content.getRoleids());
        }
        return getMapper().selectSysRoleList(qw);
    }
}
